package com.zhuiyun.educations.api.User.controller;
import com.zhuiyun.educations.api.User.entity.User;
import com.zhuiyun.educations.api.User.entity.UserDTO;
import com.zhuiyun.educations.api.User.service.UserService;
import com.zhuiyun.educations.api.User.service.impl.CodeSender;
import com.zhuiyun.educations.common.response.CommonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author 12295
 * @Date 2023/6/15 16:23
 **/
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    /**
    * @Author yangze
    * @Description TODO 邮箱发送验证码
    * @Date 2023/6/16 14:56
    * @Return
    */
    @GetMapping("/sendEmail")
    public CommonResult register(String email) {
        if (email == null) {
            return CommonResult.error("邮箱不能为空");
        }

        return CommonResult.ok(userService.sendEmail(email));
    }
    /**
    * @Author yangze
    * @Description  TODO  校验账号是否存在
    * @Date 2023/6/19 11:43
    * @Return CommonResult
    * @param loginName
    */
    @GetMapping("/getUserByLoginName")
    public CommonResult getUserByLoginName(String loginName){

        return CommonResult.ok(userService.getUserByLoginName(loginName));
    }
    /**
    * @Author yangze
    * @Description  TODO 校验名字是否存在
    * @Date 2023/6/19 19:53
    * @Return CommonResult
    * @param userName
    */
    @GetMapping("/getUserByUserName")
    public CommonResult getUserByUserName(String userName){

        return CommonResult.ok(userService.getUserByUserName(userName));
    }
    /**
    * @Author yangze
    * @Description  TODO 校验邮箱是否存在
    * @Date 2023/6/19 20:08
    * @Return
    * @param
    */

    @GetMapping("/getUserByEmail")
    public CommonResult getUserByEmail(String email){

        return CommonResult.ok(userService.getUserByEmail(email));
    }
    /**
    * @Author yangze
    * @Description   TODO 注册
    * @Date 2023/6/19 20:59
    * @Return
    * @param
    */

    @PostMapping("/register")
    public CommonResult register(@RequestBody UserDTO userDTO){
        User user = userDTO.getUser();
        String code = userDTO.getCode();
        return CommonResult.ok(userService.register(user,code));
    }


    /**
    * @Author yangze
    * @Description  TODO 根据邮箱发送验证码找回密码
    * @Date 2023/6/20 14:16
    * @Return
    * @param
    */

    @PostMapping("/findPassword")
    public  CommonResult findPassword(String email){

        Object userByEmail = userService.getUserByEmail(email);


        if (userByEmail!= null){
            return CommonResult.ok( userService.sendEmail(email));
        }
        return CommonResult.error("邮箱不存在请先注册");
    }

    /**
     * @Author cjl
     * @Description 根据用户id查询信息(除了密码)
     * @Date 2023/6/20 19:19
     * @Return com.zhuiyun.educations.common.response.CommonResult
     * @param id
     */
    @GetMapping("/selectUserbyId")
    public CommonResult selectUserbyId(Integer id) {
        return userService.selectUserbyId(id);
    }

    /**
    * @Author cjl
    * @Description  根据id修改用户信息(除了密码)
    * @Date 2023/6/21 17:57
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param user
    */
    @PutMapping("/updateUserByLoginNameById")
    public CommonResult updateUserByLoginNameById(User user) {
        return userService.updateUserByLoginNameById(user);
    }
    @PostMapping("/updateUserById")
    public CommonResult updateUserById(MultipartFile imageFile, User user) {
        if(user.getId()!=null&&user.getId()!=0){
            if (user.getUpdateTime() != null) {
                // 进行日期字段的验证和处理
                // ...
                return userService.updateUserById(imageFile,user);
            }
            user.setUpdateTime(new Date());
            return userService.updateUserById(imageFile,user);
        }
        return CommonResult.error(500,"id为null");

    }
    /**
     * @Author cjl
     * @Description 根据电话查询是否绑定了手机号
     * @Date 2023/6/23 9:02
     * @Return com.zhuiyun.educations.common.response.CommonResult
     * @param phone
     */
    @GetMapping("/selectUserByPhone")
    public CommonResult selectUserByPhone (String phone){
        return userService.selectUserByPhone(phone);
    }
    /**
    * @Author cjl
    * @Description 根据邮箱查询是否重复的邮箱
    * @Date 2023/6/23 14:11
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param email
    */
    @GetMapping("/selectUserByEmail")
    public CommonResult selectUserByEmail (String email){
        return userService.selectUserByEmail(email);
    }
    /**
    * @Author cjl
    * @Description 根据登录名查询是否重复的名称
    * @Date 2023/6/23 14:11
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param loginName
    */
    @GetMapping("/selectUserByLoginName")
    public CommonResult selectUserByLoginName (String loginName){
        return userService.selectUserByLoginName(loginName);
    }


    @PostMapping("/sendVerificationCode")
    public CommonResult sendVerificationCode (String phone){
        return userService.sendVerificationCode(phone);
    }

    @PostMapping("/verifyPhone")
    public CommonResult verifyPhone(String phone,String verificationCode){
        return userService.verifyPhone(phone,verificationCode);
    }
    /**
    * @Author yangze
    * @Description   发送手机验证码
    * @Date 2023/7/6 19:55
    * @Return
    * @param phone
    */

    @GetMapping("/sendPhoneCode")
    public CommonResult sendPhoneCode(String phone) {

        boolean success = CodeSender.sendSms(phone);

        if (success) {
            return CommonResult.ok();
        } else {
            return CommonResult.error();
        }
    }


    @PostMapping("/findPasswordByEmail")
    public CommonResult findPasswordByEmail(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = request.get("code");
        String password = request.get("password");
        return userService.findPassword(email, code, password);
    }

}
